package love.unknown.fruit.controllers;

import love.unknown.fruit.dao.FruitDao;
import love.unknown.fruit.dao.impl.FruitDAOImpl;
import love.unknown.fruit.pojo.Fruit;
import love.unknown.myssm.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class FruitController {
    private FruitDao fruitDao = new FruitDAOImpl();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
    }

    private String index(String oper, String keyword, Integer pageNo, HttpServletRequest req) {
        HttpSession session = req.getSession();
        //若oper!=null 说明 通过表单的查询按钮点击过来
        //若oper==null 说明 不是通过查询按钮过来的

        if (pageNo == null)
            pageNo = 1;

        if (StringUtil.isNotEmpty(oper) && "search".equals(oper)) {
            //此时是通过查询发送来的请求
            //此时pageNo为1 keyword为请求参数获取
            pageNo = 1;
            if (StringUtil.isEmpty(keyword)) {
                keyword = "";
            }
            session.setAttribute("keyword", keyword);
        } else {
            Object keywordObj = session.getAttribute("keyword");
            if (keywordObj != null) {
                keyword = (String) keywordObj;
            } else {
                keyword = "";
            }
        }
        session.setAttribute("pageNo", pageNo);
        FruitDao fruitDao = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDao.getFruitList(keyword, pageNo);
        System.out.println(keyword);
        //保存到session作用域
        session.setAttribute("fruitList", fruitList);
        int fruitCount = fruitDao.getFruitCount(keyword);
        int pageCount = (fruitCount + 5 - 1) / 5;
        session.setAttribute("pageCount", pageCount);
        //super.processTemplate("index", req, resp);
        return "index";
    }

    private String add(String fname, Integer price, Integer fcount, String remark) {
        Fruit fruit = new Fruit(0, fname, price, fcount, remark);
        fruitDao.addFruit(fruit);
        //resp.sendRedirect("fruit.do");
        return "redirect:fruit.do";
    }

    private String del(Integer fid) {
        if (fid != null) {
            fruitDao.delFruit(fid);
            //resp.sendRedirect("fruit.do");
            return "redirect:fruit.do";
        }
        return "error";
    }

    private String edit(Integer fid, HttpServletRequest req) {
        if (fid != null) {
            Fruit fruit = fruitDao.getFruitByFid(fid);
            req.setAttribute("fruit", fruit);
            //super.processTemplate("edit",req,resp);
            return "edit";
        }
        return "error";
    }

    private String update(Integer fid, String fname, Integer price, Integer fcount, String remark) {
        fruitDao.updateFruit(new Fruit(fid, fname, price, fcount, remark));
        //super.processTemplate("index",req,resp);
        //resp.sendRedirect("fruit.do");
        return "redirect:fruit.do";
    }
}
